package com.shengdong.water.base

import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.shengdong.water.code.base.BaseActivity
import com.shengdong.water.code.utils.StatusBarUtil
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

// BaseActivity.kt

abstract class BaseVMActivity<VB : ViewDataBinding, VM : BaseViewModel> : BaseActivity<VB>() {

    protected lateinit var viewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        observeData()
        initViews()
        initData()
        lifecycleScope.launch {
            viewModel.uiState.collect { state ->
                handleUIState(state)
            }
        }


    }

        open fun initViews() {}

        /**
         * 初始化data相关
         */
        open fun initData() {}


    open fun observeData() {}
    override fun initContentView() {
        super.initContentView()
        viewModel = ViewModelProvider(this)[getViewModelClass()]
    }

    private fun handleUIState(state: UiState) {
        when (state) {
            is UiState.ShowLoading -> showLoading(state.loading)
            is UiState.DismissLoading -> dismissLoading()
            is UiState.Error -> showError(state.message)
            else -> {}
        }
    }

    abstract fun getViewModelClass(): Class<VM> // 返回ViewModel类

}